package org.jeecg.common.modules.patient.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.jeecg.common.modules.patient.entity.Patient;
import org.jeecg.common.modules.patient.mapper.PatientMapper;
import org.jeecg.common.modules.patient.service.IPatientService;
import org.jeecg.common.modules.patient.vo.PatientReqVo;
import org.jeecg.common.modules.wx.entity.WxUserinfo;
import org.jeecg.common.util.enums.PatientRelationEnum;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.Collection;
import java.util.Date;
import java.util.List;
import java.util.Objects;

/**
 * @Description: patient
 * @Author: jeecg-boot
 * @Date:   2020-08-19
 * @Version: V1.0
 */
@Service
public class PatientServiceImpl extends ServiceImpl<PatientMapper, Patient> implements IPatientService {
    @Resource
    private PatientMapper patientMapper;

    @Override
    public List<Patient> getBetweenInfo(Integer start,Integer end){
        LambdaQueryWrapper<Patient> query = Wrappers.lambdaQuery();
        query.between(Patient::getId,start,end);
        return list(query);
    }

    @Override
    public Patient getOrCreatePatient(WxUserinfo userinfo) {
        LambdaQueryWrapper<Patient> query = Wrappers.lambdaQuery();
        query.eq(Patient::getWxUserinfoId,userinfo.getId());
        query.eq(Patient::getRelation,PatientRelationEnum.SELF.getCode());
        Patient patient = getOne(query);
        if (Objects.nonNull(patient)){
            return patient;
        }
        patient = new Patient();
        patient.setName(userinfo.getNickname());
        patient.setWxUserinfoId(userinfo.getId());
        patient.setHeadImg(userinfo.getHeadimgurl());
        patient.setPyinShort(userinfo.getPyinShort());
        patient.setFirstChar(userinfo.getFirstChar());
        patient.setRelation(PatientRelationEnum.SELF.getCode());
        patient.setSex(userinfo.getSex());
        patient.setCreateTime(new Date());
        save(patient);
        return patient;
    }

    @Override
    public Patient getSelfByWxUserinfoId(String wxUserinfoId) {
        LambdaQueryWrapper<Patient> query = Wrappers.lambdaQuery();
        query.eq(Patient::getWxUserinfoId,wxUserinfoId);
        query.eq(Patient::getRelation,PatientRelationEnum.SELF.getCode());
        return getOne(query);
    }

    @Override
    public List<Patient> getByIds(Collection<String> ids) {
        return getBaseMapper().selectBatchIds(ids);
    }

    @Override
    public List<Patient> getByWxUserInfoId(String id) {
        LambdaQueryWrapper<Patient> query = Wrappers.lambdaQuery();
        query.eq(Patient::getWxUserinfoId,id);
        return list(query);
    }

    @Override
    public List<PatientReqVo> getPatientInfos(String name, String nickname, String mobile, Integer dataStart, Integer pageSize){
        return patientMapper.selectPatient(name,nickname, mobile, dataStart, pageSize);
    }

    @Override
    public int getCount(String name, String nickname, String mobile){
        return patientMapper.getCount(name,nickname, mobile);
    }
}
